package com.shop2cn.iapi.agentproducts.testcase;

import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.GetChildSelectionConditionBean;
import com.shop2cn.iapi.agentproducts.parameter.SaveSelectionConditionBean;
import com.shop2cn.iapi.agentproducts.service.GetChildSelectionConditionCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.godmch.mch_depot_info;
/**
*查询为下级设置的选货筛选条件Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetChildSelectionCondition{
	private static GetChildSelectionConditionBean getchildselectionconditionBean;
	private static GetChildSelectionConditionCall getchildselectionconditionCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询为下级设置的选货筛选条件Test");
	}

	@Before
	public void caseUp() {
		getchildselectionconditionBean=new GetChildSelectionConditionBean();
		getchildselectionconditionCall=new GetChildSelectionConditionCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getchildselectionconditionCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private int MchId=501163178;
	private int AgentId = 501163179;
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetChildSelectionCondition_001() {
		Logger.start(true,"设置为无条件，成功");
		try {
			SaveSelectionConditionBean initSaveSelectionCondition = AddAgentProductCallService.InitSaveSelectionCondition(MchId, AgentId, 0);
			AddAgentProductCallService.SaveSelectionCondition(initSaveSelectionCondition);
			getchildselectionconditionBean.setUserId(MchId);
			getchildselectionconditionBean.setSubAgentId(AgentId);
			getchildselectionconditionCall.setData(getchildselectionconditionBean);
			getchildselectionconditionCall.callService();			
			Logger.verifyEquals("200", getchildselectionconditionCall.getString("code"), "验证code");
			JsonObject data = getchildselectionconditionCall.getJsonObject().get("data").getAsJsonObject();
			Logger.verifyEquals(0, data.get("scope").getAsInt(),"验证scope");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetChildSelectionCondition_002() {
		Logger.start(true,"查询按照品牌的规则");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(MchId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			SaveSelectionConditionBean initSaveSelectionCondition = AddAgentProductCallService.InitSaveSelectionCondition(MchId, AgentId, 1);
			initSaveSelectionCondition.setBrandIdList(Arrays.asList(addAgentProductBean.getBrandId()));
			AddAgentProductCallService.SaveSelectionCondition(initSaveSelectionCondition);
			getchildselectionconditionBean.setUserId(MchId);
			getchildselectionconditionBean.setSubAgentId(AgentId);
			getchildselectionconditionCall.setData(getchildselectionconditionBean);
			getchildselectionconditionCall.callService();			
			Logger.verifyEquals("200", getchildselectionconditionCall.getString("code"), "验证code");
			JsonObject data = getchildselectionconditionCall.getJsonObject().get("data").getAsJsonObject();
			Logger.verifyEquals(1, data.get("scope").getAsInt(),"验证scope");
			Logger.verifyEquals(addAgentProductBean.getBrandId(), data.getAsJsonArray("brandIdList").get(0).getAsInt(),"验证brandId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetChildSelectionCondition_003() {
		Logger.start(true,"查询按照类目的规则");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(MchId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			SaveSelectionConditionBean initSaveSelectionCondition = AddAgentProductCallService.InitSaveSelectionCondition(MchId, AgentId, 1);
			initSaveSelectionCondition.setFirstCategoryIdList(Arrays.asList(addAgentProductBean.getCategoryId()));
			
			AddAgentProductCallService.SaveSelectionCondition(initSaveSelectionCondition);
			getchildselectionconditionBean.setUserId(MchId);
			getchildselectionconditionBean.setSubAgentId(AgentId);
			getchildselectionconditionCall.setData(getchildselectionconditionBean);
			getchildselectionconditionCall.callService();			
			Logger.verifyEquals("200", getchildselectionconditionCall.getString("code"), "验证code");
			JsonObject data = getchildselectionconditionCall.getJsonObject().get("data").getAsJsonObject();
			Logger.verifyEquals(1, data.get("scope").getAsInt(),"验证scope");
			Logger.verifyEquals(addAgentProductBean.getCategoryId(), data.getAsJsonArray("firstCategoryIdList").get(0).getAsInt(),"验证firstCategoryId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetChildSelectionCondition_004() {
		Logger.start(true,"查询按照仓库的规则");
		try {
			List<mch_depot_info> selectDepotByDepotTitle = GodmchManageCallService.SelectDepotByMchId(MchId);
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(MchId, "", true);
			addAgentProductBean.setDepotId(selectDepotByDepotTitle.get(0).getDepotId());
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			SaveSelectionConditionBean initSaveSelectionCondition = AddAgentProductCallService.InitSaveSelectionCondition(MchId, AgentId, 1);
			initSaveSelectionCondition.setDepotIdList(Arrays.asList(addAgentProductBean.getDepotId()));
			AddAgentProductCallService.SaveSelectionCondition(initSaveSelectionCondition);
			getchildselectionconditionBean.setUserId(MchId);
			getchildselectionconditionBean.setSubAgentId(AgentId);
			getchildselectionconditionCall.setData(getchildselectionconditionBean);
			getchildselectionconditionCall.callService();			
			Logger.verifyEquals("200", getchildselectionconditionCall.getString("code"), "验证code");
			JsonObject data = getchildselectionconditionCall.getJsonObject().get("data").getAsJsonObject();
			Logger.verifyEquals(1, data.get("scope").getAsInt(),"验证scope");
			Logger.verifyEquals(addAgentProductBean.getDepotId(), data.getAsJsonArray("depotIdList").get(0).getAsInt(),"验证depotId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetChildSelectionCondition_005() {
		Logger.start(true,"查询全部规则");
		try {
			List<mch_depot_info> selectDepotByDepotTitle = GodmchManageCallService.SelectDepotByMchId(MchId);
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(MchId, "", true);
			addAgentProductBean.setDepotId(selectDepotByDepotTitle.get(0).getDepotId());
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			SaveSelectionConditionBean initSaveSelectionCondition = AddAgentProductCallService.InitSaveSelectionCondition(MchId, AgentId, 1);
			initSaveSelectionCondition.setBrandIdList(Arrays.asList(addAgentProductBean.getBrandId()));
			initSaveSelectionCondition.setFirstCategoryIdList(Arrays.asList(addAgentProductBean.getCategoryId()));
			initSaveSelectionCondition.setDepotIdList(Arrays.asList(addAgentProductBean.getDepotId()));
			AddAgentProductCallService.SaveSelectionCondition(initSaveSelectionCondition);
			getchildselectionconditionBean.setUserId(MchId);
			getchildselectionconditionBean.setSubAgentId(AgentId);
			getchildselectionconditionCall.setData(getchildselectionconditionBean);
			getchildselectionconditionCall.callService();			
			Logger.verifyEquals("200", getchildselectionconditionCall.getString("code"), "验证code");
			JsonObject data = getchildselectionconditionCall.getJsonObject().get("data").getAsJsonObject();
			Logger.verifyEquals(1, data.get("scope").getAsInt(),"验证scope");
			Logger.verifyEquals(addAgentProductBean.getBrandId(), data.getAsJsonArray("brandIdList").get(0).getAsInt(),"验证brandId");
			Logger.verifyEquals(addAgentProductBean.getCategoryId(), data.getAsJsonArray("firstCategoryIdList").get(0).getAsInt(),"验证firstCategoryId");
			Logger.verifyEquals(addAgentProductBean.getDepotId(), data.getAsJsonArray("depotIdList").get(0).getAsInt(),"验证depotId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetChildSelectionCondition_007() {
		Logger.start(true,"商户与代理未设置规则");
		try {
			//创建一个商户和分销小店
			Integer MchId = GodmchManageCallService.CreateGodMch();
			GodmchManageCallService.SetVip(MchId);
			Integer AgentId = GodmchManageCallService.CreateAgent(MchId);
			
			getchildselectionconditionBean.setUserId(MchId);
			getchildselectionconditionBean.setSubAgentId(AgentId);
			getchildselectionconditionCall.setData(getchildselectionconditionBean);
			getchildselectionconditionCall.callService();			
			Logger.verifyEquals("200", getchildselectionconditionCall.getString("code"), "验证code");
			JsonObject data = getchildselectionconditionCall.getJsonObject().get("data").getAsJsonObject();
			Logger.verifyEquals(0, data.get("scope").getAsInt(),"验证scope");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetChildSelectionCondition_008() {
		Logger.start(true,"商户为空，失败");
		try {
			getchildselectionconditionBean.setUserId(null);
			getchildselectionconditionBean.setSubAgentId(AgentId);
			getchildselectionconditionCall.setData(getchildselectionconditionBean);
			getchildselectionconditionCall.callService();			
			Logger.verifyEquals("400", getchildselectionconditionCall.getString("code"), "验证code");
			Logger.verifyEquals("userId:商户id必传", getchildselectionconditionCall.getString("message"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	
	
	
	
	
}